package com.us.usws.support.uitls;

import java.util.Calendar;

import org.apache.commons.codec.digest.DigestUtils;

/**
 *
 * @author Wang Jianbo
 */
public class ResourceAuditKeyUtils {

	private static ResourceAuditKeyUtils instance = new ResourceAuditKeyUtils();

	private static String SALT_PREFIX = "I@wsh#0927#yE5";
	private static String SALT_SUFFIX = "..|vsws";

	private ResourceAuditKeyUtils() {
	}

	public static void main(String[] args) {
		ResourceAuditKeyUtils raku = ResourceAuditKeyUtils.getInstance();
		String s = "10310000000549";
		String auditKey = "69ed66";
		System.out.println(raku.getAuditKey(s));
		System.out.println(raku.validateAuditKey(s, auditKey));
	}

	public static ResourceAuditKeyUtils getInstance() {
		return instance;
	}

	public boolean validateAuditKey(String s, String auditKey) {
		return auditKey.equals(getAuditKey(s));
	}

	/**
	 * get audit key by salt
	 * @param s
	 * @return
	 */
	public String getAuditKey(String s) {
		String ret = DigestUtils.md5Hex(salt(s));
		return ret.substring(0, 3) + ret.substring(ret.length() - 3);
	}

	/**
	 * private method, add salt
	 * @param s
	 * @return
	 */
	private String salt(String s) {
		StringBuilder sb = new StringBuilder(SALT_PREFIX);
		sb.append(s);

		Calendar cal = Calendar.getInstance();

		sb.append(cal.get(Calendar.YEAR));
		sb.append(cal.get(Calendar.MONTH));
		sb.append(cal.get(Calendar.DATE));

		sb.append(SALT_SUFFIX);
		return sb.toString();
	}
}
